<?php
namespace app\index\controller;

use app\common\service\MessageService;
use think\Request;
use app\common\utils\ResultVOUtil;
use app\common\enums\ResultEnum;

class Message extends Base
{
    protected $messageService;
    
    public function __construct(MessageService $messageService)
    {
        parent::initialize();
        $this->messageService = $messageService;
    }
    
    public function send(Request $request){
        $param = $request->param();
        if(!empty($param['type'])){
            $status = $this->messageService->intercept($param['carId'], $param['type']);
            if(!$status){
                return ResultVOUtil::error(ResultEnum::OPERATE_ERROR['code'],'请勿频繁操作');
            }
            $data = $this->messageService->send($param['cardCode'], $param['content']);
            if($data){
                return ResultVOUtil::success('');
            }else{
                return ResultVOUtil::error(['code'=>ResultEnum::PARAM_ERROR['code'],'msg'=>'发送失败']);
            }
        }else{
            if(!empty($param)){
                $data = $this->messageService->send($param['cardCode'], $param['content']);
                if($data){
                    return ResultVOUtil::success('');
                }else{
                    return ResultVOUtil::error(['code'=>ResultEnum::PARAM_ERROR['code'],'msg'=>'发送失败']);
                }
            } 
        }  
    }
    
    public function cardStatus(String $simId){
        if(!empty($simId)){
            $data = $this->messageService->cardStatus($simId);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(['code'=>ResultEnum::PARAM_ERROR['code'],'msg'=>'暂无数据']);
            }
        }else{
            return ResultVOUtil::error(['code'=>ResultEnum::PARAM_ERROR['code']]);
        }
    }
    
    public function smsup(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $data = $this->messageService->smsup($param['page'], $param['length'], $param['search']);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(['code'=>ResultEnum::PARAM_ERROR['code'],'msg'=>'暂无数据']);
            }
        }else{
            return ResultVOUtil::error(['code'=>ResultEnum::PARAM_ERROR['code']]);
        }
    }

    public function smsDown(Request $request){
        $param = $request->param();
        if(!empty($param)){
            $data = $this->messageService->smsDown($param['page'],$param['length'],$param['search']);
            if($data){
                return ResultVOUtil::success($data);
            }else{
                return ResultVOUtil::error(['code'=>ResultEnum::PARAM_ERROR['code'],'msg'=>'暂无数据']);
            }
            
        }else{
            return ResultVOUtil::error(['code'=>ResultEnum::PARAM_ERROR['code']]);
        }
    }
    
    public function traffic($simId="",$simCard=""){
        $data = $this->messageService->traffic($simId, $simCard);
        if($data){
            return ResultVOUtil::success($data);
        }else{
            return ResultVOUtil::error(['code'=>ResultEnum::PARAM_ERROR['code'],'msg'=>'暂无数据']);
        }
    }
    
    public function packageInfo($simId="",$simCard=""){
        $data = $this->messageService->packageInfo($simId, $simCard);
        if($data){
            return ResultVOUtil::success($data);
        }else{
            return ResultVOUtil::error(['code'=>ResultEnum::PARAM_ERROR['code'],'msg'=>'暂无数据']);
        }
    }
    
    public function upgrade($id=0){
        $data = $this->messageService->upgrade($id);
        if($data){
            return ResultVOUtil::success('');
        }else{
            return ResultVOUtil::error(ResultEnum::ERROR);
        }
    }
    
    public function queryStatus($sendKey=""){
        $data = $this->messageService->queryStatus($sendKey);
         if($data){
             return ResultVOUtil::success($data);
         }else{
             return ResultVOUtil::error(ResultEnum::ERROR);
         }
    }
    
}